NAME = libbag

CC ?= gcc
RM ?= @rm
MKDIR ?= @mkdir

CFLAGS := -O3 -Wall -Wextra -fopenmp

SRC_DIR = src
BUILD_DIR = build
DATA_DIR = data
INCLUDES = src

OBJECTS = $(NAME).o simple.o cbag.o
PLOTS = simple_prod_cons_avg_plot.tex simple_1_prod_avg_plot.tex simple_1_cons_avg_plot.tex simple_add_tra_avg_plot.tex simple_add_avg_plot.tex simple_tra_avg_plot.tex cbag_prod_cons_avg_plot.tex cbag_1_prod_avg_plot.tex cbag_1_cons_avg_plot.tex cbag_add_tra_avg_plot.tex cbag_add_avg_plot.tex cbag_tra_avg_plot.tex
SMALLPLOTS = simple_small_prod_cons_avg_plot.tex simple_small_1_prod_avg_plot.tex simple_small_1_cons_avg_plot.tex simple_small_add_tra_avg_plot.tex simple_small_add_avg_plot.tex simple_small_tra_avg_plot.tex cbag_small_prod_cons_avg_plot.tex cbag_small_1_prod_avg_plot.tex cbag_small_1_cons_avg_plot.tex cbag_small_add_tra_avg_plot.tex cbag_small_add_avg_plot.tex cbag_small_tra_avg_plot.tex

all: $(BUILD_DIR) $(NAME) $(NAME).so
	@echo "Built $(NAME)"

$(DATA_DIR):
	@echo "Creating data directory: $(DATA_DIR)"
	$(MKDIR) $(DATA_DIR)

$(BUILD_DIR):
	@echo "Creating build directory: $(BUILD_DIR)"
	$(MKDIR) $(BUILD_DIR)

$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
	@echo "Compiling $<"
	$(CC) $(CFLAGS) -fPIC -I$(INCLUDES) -c -o $@ $<

$(NAME): $(foreach object,$(OBJECTS),$(BUILD_DIR)/$(object))
	@echo "Linking $(NAME)"
	$(CC) $(CFLAGS) -o $@ $^

$(NAME).so: $(foreach object,$(OBJECTS),$(BUILD_DIR)/$(object))
	@echo "Linking $(NAME)"
	$(CC) $(CFLAGS) -fPIC -shared -o $@ $^ 

small-bench: $(BUILD_DIR) $(NAME).so $(DATA_DIR)
	@echo "Running small-bench ..."
	@echo "Note: to run on slurm, use target `slurm-small-bench`"
	@python benchmark.py
	@echo "Finished small-bench"

bench:
	@echo "Running bench (detailed benchmark) ..."
	@echo "Note: to run on slurm, use target `slurm-bench`"
	@python benchmark.py --threads 2,4,8,16,32,64 --workload 10000,100000,1000000 --repetitions 4 --prefix ""
	@echo "Finished bench"

slurm-small-bench: $(BUILD_DIR) $(NAME).so $(DATA_DIR)
	@echo "Running slurm-small-bench ..."
	@srun -p q_student -t 10 -N 1 -c 64 python benchmark.py
	@echo "Finished slurm-small-bench"

slurm-bench:
	@echo "Running bench (detailed benchmark) ..."
	@echo " (only simple)"
	@srun -p q_student -t 10 -N 1 -c 64 python benchmark.py --threads 2,4,8,16,32,64 --workload 10000 --repetitions 16 --prefix "" --cbag false --simple true
	@srun -p q_student -t 10 -N 1 -c 64 python benchmark.py --threads 2,4,8,16,32,64 --workload 100000 --repetitions 16 --prefix "" --cbag false --simple true --prod_cons true --1_cons false --1_prod false --add_try_remove_any false
	@srun -p q_student -t 10 -N 1 -c 64 python benchmark.py --threads 2,4,8,16,32,64 --workload 100000 --repetitions 16 --prefix "" --cbag false --simple true --prod_cons false --1_cons true --1_prod false --add_try_remove_any false
	@srun -p q_student -t 10 -N 1 -c 64 python benchmark.py --threads 2,4,8,16,32,64 --workload 100000 --repetitions 4 --prefix "" --cbag false --simple true --prod_cons false --1_cons false --1_prod true --add_try_remove_any false
	@srun -p q_student -t 10 -N 1 -c 64 python benchmark.py --threads 2,4,8,16,32,64 --workload 100000 --repetitions 16 --prefix "" --cbag false --simple true --prod_cons false --1_cons false --1_prod false --add_try_remove_any true
	@srun -p q_student -t 10 -N 1 -c 64 python benchmark.py --threads 2,4,8,16,32,64 --workload 1000000 --repetitions 8 --prefix "" --cbag false --simple true --prod_cons true --1_cons false --1_prod false --add_try_remove_any false
	@srun -p q_student -t 10 -N 1 -c 64 python benchmark.py --threads 2,4,8,16,32,64 --workload 1000000 --repetitions 8 --prefix "" --cbag false --simple true --prod_cons false --1_cons true --1_prod false --add_try_remove_any false
	@srun -p q_student -t 10 -N 1 -c 64 python benchmark.py --threads 2,4,8,16,32,64 --workload 1000000 --repetitions 2 --prefix "" --cbag false --simple true --prod_cons false --1_cons false --1_prod true --add_try_remove_any false
	@srun -p q_student -t 10 -N 1 -c 64 python benchmark.py --threads 2,4,8,16,32,64 --workload 1000000 --repetitions 8 --prefix "" --cbag false --simple true --prod_cons false --1_cons false --1_prod false --add_try_remove_any true
	@echo " (only cbag)"
	@srun -p q_student -t 10 -N 1 -c 64 python benchmark.py --threads 2,4,8,16,32,64 --workload 10000 --repetitions 16 --prefix "" --cbag true --simple false
	@srun -p q_student -t 10 -N 1 -c 64 python benchmark.py --threads 2,4,8,16,32,64 --workload 100000 --repetitions 16 --prefix "" --cbag true --simple false --prod_cons true --1_cons false --1_prod false --add_try_remove_any false
	@srun -p q_student -t 10 -N 1 -c 64 python benchmark.py --threads 2,4,8,16,32,64 --workload 100000 --repetitions 16 --prefix "" --cbag true --simple false --prod_cons false --1_cons true --1_prod false --add_try_remove_any false
	@srun -p q_student -t 10 -N 1 -c 64 python benchmark.py --threads 2,4,8,16,32,64 --workload 100000 --repetitions 4 --prefix "" --cbag true --simple false --prod_cons false --1_cons false --1_prod true --add_try_remove_any false
	@srun -p q_student -t 10 -N 1 -c 64 python benchmark.py --threads 2,4,8,16,32,64 --workload 100000 --repetitions 16 --prefix "" --cbag true --simple false --prod_cons false --1_cons false --1_prod false --add_try_remove_any true
	@srun -p q_student -t 10 -N 1 -c 64 python benchmark.py --threads 2,4,8,16,32,64 --workload 1000000 --repetitions 8 --prefix "" --cbag true --simple false --prod_cons true --1_cons false --1_prod false --add_try_remove_any false
	@srun -p q_student -t 10 -N 1 -c 64 python benchmark.py --threads 2,4,8,16,32,64 --workload 1000000 --repetitions 8 --prefix "" --cbag true --simple false --prod_cons false --1_cons true --1_prod false --add_try_remove_any false
	@srun -p q_student -t 10 -N 1 -c 64 python benchmark.py --threads 2,4,8,16,32,64 --workload 1000000 --repetitions 2 --prefix "" --cbag true --simple false --prod_cons false --1_cons false --1_prod true --add_try_remove_any false
	@srun -p q_student -t 10 -N 1 -c 64 python benchmark.py --threads 2,4,8,16,32,64 --workload 1000000 --repetitions 8 --prefix "" --cbag true --simple false --prod_cons false --1_cons false --1_prod false --add_try_remove_any true
	@echo "Finished slurm-bench"

small-plot: 
	@echo "Plotting small-bench results ..."
	bash -c 'cd plots && for tex in $(SMALLPLOTS); do \
		pdflatex "\newcommand{\DATAPATH}{../data/$$(ls ../data/ | sort -r | head -n 1)}\input{$$tex}"; \
		echo "created plot for $$tex"; \
	done'
	@echo "============================================"
	@echo "Done"

plot: 
	@echo "Plotting bench results ..."
	bash -c 'cd plots && for tex in $(PLOTS); do \
		pdflatex "\newcommand{\DATAPATH}{../data/$$(ls ../data/ | sort -r | head -n 1)}\input{$$tex}"; \
		echo "created plot for $$tex"; \
	done'
	@echo "============================================"
	@echo "Done"

report: plot
	@echo "Compiling report ..."
	bash -c 'cd report && pdflatex report.tex && pdflatex report.tex'
	@echo "============================================"
	@echo "Done"

zip:
	@zip framework.zip benchmark.py Makefile README src/* plots/avg_plot.tex report/report.tex

clean:
	@echo "Cleaning build directory: $(BUILD_DIR) and binaries: $(NAME) $(NAME).so"
	$(RM) -Rf $(BUILD_DIR)
	$(RM) -f $(NAME) $(NAME).so
	$(RM) -f plots/*.aux plots/*.log plots/*.fls plots/*.synctex.gz plots/*.fdb_latexmk plots/*.pdf report/*.aux report/*.fdb_latexmk report/*.fls report/*.log report/*.out report/*.pdf

.PHONY: clean report
